<?php
if (!defined('SITE_PATH')) {
    exit();
}
define('INDEX_IMAGE_NUM', 5);//首页图片
require(SITE_PATH . 'system/common.php');

/**
 * 项目访问记录
 * @param $deal_id
 */
function log_deal_visit($deal_id)
{
    if (check_ipop_limit(get_client_ip(), "deal_show", 600, $deal_id)) {
        $total = M('deal_visit_log')->where(array('deal_id' => $deal_id, 'client_ip' => get_client_ip(), 'create_time' => array('EGT', TIME_NOW - 600)))->count();
        if ($total == 0) {
            $view_data['deal_id'] = $deal_id;
            $view_data['client_ip'] = get_client_ip();
            $view_data['create_time'] = TIME_NOW;
            M('deal_visit_log')->data($view_data)->add();
            M('deal')->where(array('id' => $deal_id))->data(array('view_count' => array('exp', 'view_count+1')))->save();
        }
    }
}

/**
 * 金额格式化
 * @param $price
 * @return string
 */
function number_price_format($price)
{
    if ($price * 100 % 100 == 0) {
        $price = number_format(round($price, 2));
    } else {
        $price = number_format(round($price, 2), 2);
    }
    return $price;
}

/**
 * 上传文件-webuploader
 * @param string $name 表单名称
 * @param string $path 上传保存路径
 * @param string $web_url WEB访问路径
 * @param string $filename 保存文件名
 * @param string $allowType 允许上传文件类型
 * @return mixed
 */

function webuploader($name,$path,$web_url,$filename=null,$allowType='jpeg,jpg,jpe,gif,png')
{
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
        exit();
    }
    if (!empty($_REQUEST['debug'])) {
        $random = rand(0, intval($_REQUEST['debug']));
        if ($random === 0) {
            header("HTTP/1.0 500 Internal Server Error");
            exit;
        }
    }
    $allowTypeList = explode(',',$allowType);
    @set_time_limit(60 * 60);
    $uploadDir = $path;
    $targetDir = $uploadDir.'../temp/';
    if (!is_dir($targetDir)) {
        @mkdir($targetDir,0777,true);
    }
    if (!is_dir($uploadDir)) {
        @mkdir($uploadDir,0777,true);
    }
    if (isset($_REQUEST[$name])) {
        $fileName = $_REQUEST[$name];
    } elseif (!empty($_FILES)) {
        $fileName = $_FILES["file"][$name];
    } else {
        $fileName = uniqid("file_");
    }
    $fileType = pathinfo($fileName);
    if(!in_array(strtolower($fileType['extension']),$allowTypeList)){
        $status  = array('status'=>0,'info'=>'{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "File Type Not Allow Upload."}, "id" : "id"}');
        return $status;
    }
    $uploadPath = $uploadDir . $filename;
    if (!$out = @fopen($uploadPath, "wb")) {
        $status  = array('status'=>0,'info'=>'{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
        return $status;
    }
    if (!empty($_FILES)) {
        if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
            $status  = array('status'=>0,'info'=>'{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
            return $status;
        }
        if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
            $status  = array('status'=>0,'info'=>'{"jsonrpc" : "2.0", "error" : {"code": 101 "message": "Failed to open input stream."}, "id" : "id"}');
            return $status;
        }
    } else {
        if (!$in = @fopen("php://input", "rb")) {
            $status  = array('status'=>0,'info'=>'{"jsonrpc" : "2.0", "error" : {"code": 101 "message": "Failed to open input stream."}, "id" : "id"}');
            return $status;
        }
    }
    while ($buff = fread($in, 4096)) {
        fwrite($out, $buff);
    }
    @fclose($out);
    @fclose($in);
    $status  = array('status'=>1,'info'=>'{"jsonrpc" : "2.0", "result" : "'.$web_url.'", "id" : "id"}');
    return $status;
}

?>